package ballpool;

import game.GameMap;

import java.util.ArrayList;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.MouseListener;
import org.newdawn.slick.SlickException;

public class BallpoolGame extends BasicGame implements MouseListener {
	private ArrayList<Ball> balls;
	private int width = 640, height = 480;
	private long step = 0;
	private GameMap map;
	private int BALLAMOUNT = 500; //Change if your fps is low!
	
	public BallpoolGame(String title) {
		super(title);
		balls = new ArrayList<Ball>();
	}

	@Override
	public void init(GameContainer arg0) throws SlickException {
		//Add ballpool balls
		for(int i = 0; i < BALLAMOUNT; i++){
			int rX = ((int) (Math.random()*width));
			int rY = ((int) (Math.random()*height));
			//Make sure they stay in the map width and height
			rX = rX -32;
			rY = rY -32;
			if(rX < 0) rX = 0;
			if(rY < 0) rY = 0;
			//Add ball
			balls.add(new Ball(rX, rY));
		}
		map = new GameMap(width, height);
	}

	@Override
	public void update(GameContainer container, int delta)
			throws SlickException {
		for(Ball b: balls){
			if(b.getX() > width || b.getY() > height){
				balls.remove(b);
			}
			b.step(map, step);
			b.move(map);
		}
		step++;
	}

	@Override
	public void render(GameContainer container, Graphics g)
			throws SlickException {
		for(Ball b: balls){
			g.setColor(b.getColor());
			g.fillOval(b.getX(), b.getY(), b.getWidth(), b.getHeight());
		}
		//Uncomment if you want to see the collision points on the map
		/*
		for(int x = 0; x < width; x++){
			for(int y = 0; y < height; y++){
				if(this.map.hasPointAtLocation(x, y)){
					g.drawRect(x, y, 1, 1);
				}
			}
		}*/
	}
	
	public static void main(String[] args)
	{
		try { AppGameContainer app = new AppGameContainer(new BallpoolGame("Ballpool")); app.setTargetFrameRate(60); app.start();  }
			catch (SlickException e) { e.printStackTrace(); }
	}

	@Override
	public void mousePressed(int button, int x, int y){
		if(button == Input.MOUSE_LEFT_BUTTON){
		}
		else if(button == Input.MOUSE_RIGHT_BUTTON){
			this.map.clearMap();
		}
	}
	
	@Override
	public void mouseClicked(int button, int x, int y, int clickCount){
	}

	@Override
	public void mouseReleased(int button, int x, int y){
		if(button == Input.MOUSE_LEFT_BUTTON){
		}
		else if(button == Input.MOUSE_RIGHT_BUTTON){
		}
	}
	
	@Override
	public void mouseMoved(int oldx, int oldy, int newx, int newy) {
		map.clearMap();
		if(newx > 0 && newx < width && newy > 0 && newy < height){
			for(int xx = newx; xx < newx+20; xx++){
				for(int yy = newy; yy < newy+20; yy++){
					if(xx < width && yy < height) map.setPointOnMap(xx, yy, 1);
				}
			}
		}
	}
	
	@Override
	public void mouseDragged(int oldx, int oldy, int newx, int newy){
		
	}
	
}
